//
//  huiyuanDetailsViewController.m
//  daogouApp
//
//  Created by panquanReabam.com on 15-6-2.
//  Copyright (c) 2015年 jiezsoft.com. All rights reserved.
//

#import "MemberCardTopUpViewController.h"

#import "cardView.h"
#import "ToResiduePayViewController.h"
#import "MJRefresh.h"
#import "huiyuanDetailsCell.h"
#import "HZAreaPickerView.h"
#import "ZJSwitch.h"
#import "cardView.h"
#import "huiyuandengjiViewController.h"
#import "GenerateQRCodeViewController.h"
#import "SelectTag_NewViewController.h"
@interface MemberCardTopUpViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UIScrollViewDelegate,UITextViewDelegate,HZAreaPickerDelegate>

@property (strong, nonatomic) HZAreaPickerView *locatePicker;



@end

@implementation MemberCardTopUpViewController


{
    UIScrollView*myscrollView;
    HAODEsingleton* commdity;
    UIImageView* imageview;
    UILabel *lineLabel1;
    cardView *View;
    UIScrollView*mainScroll;
    int d;
    UIView*v;
    NSMutableArray*arr;
    UIButton*tureBtn;
}
@synthesize alt = alt;
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    


   
    commdity=[HAODEsingleton shared];
    self.title = @"储值卡充值";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    
    
    UIButton*name=[[UIButton alloc]initWithFrame:CGRectMake(15, 64, 200, 60)];
    [name setTitle:[NSString stringWithFormat:@"%@(%@)", commdity.userName, commdity.Phone] forState:UIControlStateNormal];
    name.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    [name setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    name.titleLabel.font=[UIFont systemFontOfSize:17];
    [self.view addSubview:name];
    UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(15, 60+64, ScreenWidth-30, 0.35)];
    line.backgroundColor=qianhuicolor;
    [self.view addSubview:line];

    UILabel*miane=[[UILabel alloc]initWithFrame:CGRectMake(15, 90+64, ScreenWidth-100, 16)];
    miane.text=@"充值面额";
    miane.textColor=qianheicolor;
    miane.font=[UIFont systemFontOfSize:15];
    [self.view addSubview:miane];
    
    [v removeFromSuperview];
    
    arr=[[NSMutableArray alloc]init];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    

    v=[[UIView alloc]initWithFrame:CGRectMake(0, 240/2, ScreenWidth, ScreenHeight-240/2-45)];
    [self.view addSubview:v];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    
    [HttpRequest requestData:@"Business/Member/CardOptions" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
              NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
          
                arr=[severDataDict.dataDict objectForKey:@"DataLine"];
                for (int i = 0,j = 0; i < arr.count; i++) {
                    if (i % 3 == 0) {
                        j++;
                    }
                 
                    View = [[cardView alloc]initWithFrame:CGRectMake(12.5+(ScreenWidth/3-5)*(i%3), (90*j), ScreenWidth/3-15, 80)];
                    View.buttonView.titleLabel.textAlignment=NSTextAlignmentCenter;
                    View.buttonView.titleLabel.font=[UIFont systemFontOfSize:24];
                    View.buttonView.tag=i;
                    
                    NSString*s=[NSString stringWithFormat:@"%@元", [[arr objectAtIndex:i] objectForKey:@"faceValue"]];
                   
                    [View.buttonView setTitle:s forState:UIControlStateNormal];
                    
                    NSMutableAttributedString *AttributedStr1= [[NSMutableAttributedString alloc]initWithString:s];
                    
                    [AttributedStr1 addAttribute:NSFontAttributeName
                     
                                          value:[UIFont systemFontOfSize:18]
                     
                                          range:NSMakeRange(s.length-1, 1)];
                    
                    [AttributedStr1 addAttribute:NSForegroundColorAttributeName
                     
                                          value:luseColor
                     
                                          range:NSMakeRange(s.length-1, 1)];
                    
                    
                    View.buttonView.titleLabel.attributedText = AttributedStr1;

               
                    
                    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
                    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){90/255.0,130/255.0, 26/255.0, 1.0   });
                    [View.buttonView.layer setBorderColor:colorref2];//边框颜色
                    [View.buttonView.layer setCornerRadius:3.0];
                    [View.buttonView.layer setBorderWidth:0.43];
                    [View.buttonView addTarget:self action:@selector(dianju:) forControlEvents:UIControlEventTouchUpInside];
                    [View.buttonView setTitleColor:luseColor forState:0];
                    
                    View.label_price.text=[NSString stringWithFormat:@"销价 ¥ %@元", [[arr objectAtIndex:i] objectForKey:@"saleValue"]];
                   
                    [v addSubview:View];
                    
                   
                    
                }
                

                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
    
    
    [tureBtn removeFromSuperview];
    tureBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenHeight-48, ScreenWidth, 48)];
    [tureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [tureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [tureBtn.layer setCornerRadius:0]; //设置矩圆角半径
//    [tureBtn.layer setBorderWidth:0];   //边框宽度
//    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
//    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){228/255.0, 228/255.0, 228/255.0, 1.0});
//    [tureBtn.layer setBorderColor:tureBTncolorref];//边框颜色
    [tureBtn addTarget:self action:@selector(ture:) forControlEvents:UIControlEventTouchUpInside];
    tureBtn.backgroundColor=huisecolor;
    [self.view addSubview:tureBtn];

    
    
    
    
    
}



-(void)dianju:(UIButton*)sender{

    NSString*tag=[NSString stringWithFormat:@"%ld",sender.tag];
    d=tag.intValue;
    [self jiazai];
    [tureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    tureBtn.backgroundColor=luseColor;
    
}


-(void)jiazai{
    [v removeFromSuperview];
    v=[[UIView alloc]initWithFrame:CGRectMake(0, 240/2, ScreenWidth, ScreenHeight-240/2-45)];
    [self.view addSubview:v];

    for (int i = 0,j = 0; i < arr.count; i++) {
        if (i % 3 == 0) {
            j++;
        }
        
        View = [[cardView alloc]initWithFrame:CGRectMake(12.5+(ScreenWidth/3-5)*(i%3), (90*j), ScreenWidth/3-15, 80)];
        View.buttonView.titleLabel.textAlignment=NSTextAlignmentCenter;
        View.buttonView.titleLabel.font=[UIFont systemFontOfSize:24];
        View.buttonView.tag=i;
        NSString*s=[NSString stringWithFormat:@"%@元", [[arr objectAtIndex:i] objectForKey:@"faceValue"]];
        
        [View.buttonView setTitle:s forState:UIControlStateNormal];
        
        NSMutableAttributedString *AttributedStr1= [[NSMutableAttributedString alloc]initWithString:s];
        
        [AttributedStr1 addAttribute:NSFontAttributeName
         
                               value:[UIFont systemFontOfSize:18]
         
                               range:NSMakeRange(s.length-1, 1)];
        
        [AttributedStr1 addAttribute:NSForegroundColorAttributeName
         
                               value:luseColor
         
                               range:NSMakeRange(s.length-1, 1)];
        
        
        View.buttonView.titleLabel.attributedText = AttributedStr1;
        CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
        CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){90/255.0,130/255.0, 26/255.0, 1.0   });
        [View.buttonView.layer setBorderColor:colorref2];//边框颜色
        [View.buttonView addTarget:self action:@selector(dianju:) forControlEvents:UIControlEventTouchUpInside];
        [View.buttonView setTitleColor:luseColor forState:0];
        [View.buttonView.layer setCornerRadius:3.0];
        [View.buttonView.layer setBorderWidth:0.43];
        View.label_price.text=[NSString stringWithFormat:@"销价 ¥ %@元", [[arr objectAtIndex:i] objectForKey:@"saleValue"]];
        
      
        
        
        if (i==d) {
            View.buttonView.backgroundColor=luseColor;
            View.label_price.textColor=[UIColor whiteColor];
            [View.buttonView setTitleColor:[UIColor whiteColor] forState:0];
        }
        
         [v addSubview:View];
    }
    



}
-(void)ture:(UIButton*)sender{
    
    
    
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* Token = [user objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            
            [params setValue:Token forKey:@"tokenId"];
            [params setValue:commdity.memberId forKey:@"memberId"];
            [params setValue:[[arr objectAtIndex:d] objectForKey:@"coId"]  forKey:@"coId"];
    
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
            
            
            [HttpRequest requestData:@"Business/Member/CardTopUp" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    
                    NSLog(@"33333333====%@",severDataDict.dataDict);
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
//                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
//                        [Global showTextHudSub:requestResultStr view:self.view];
                        
                        commdity.Dict=severDataDict.dataDict;
                        commdity.sat=@"card";
                        
                        ToResiduePayViewController  *generateVC=[[ToResiduePayViewController alloc] init];
                        generateVC.sat=@"card";
                        [self.navigationController pushViewController:generateVC animated:YES];
                        
//                        commdity.gengxin=@"";
//                        [self.navigationController popViewControllerAnimated:YES];
                        
                    }else{
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                    }
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }
                
            }];
            
            
            
    
    
}




- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
@end
